package com.platform.api.bzproject.controller;


import com.github.pagehelper.PageInfo;
import com.platform.api.bzproject.model.*;
import com.platform.api.bzproject.service.BzConfigService;
import com.platform.api.bzproject.service.ITzCollectionInfoService;
import com.platform.api.bzproject.service.IWithdrawalDetailsService;
import com.platform.api.bzproject.service.UserService;
import com.platform.common.core.Constant;
import com.platform.common.core.Result;
import com.platform.common.core.ResultCode;
import com.platform.common.util.GenerateSequenceUtil;
import com.platform.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 提现表 前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2022-01-02
 * 源码联系微信：2549789059
 */
@RestController
@RequestMapping("/u/withdrawal")
public class WithdrawalDetailsController {

    @Autowired
    private IWithdrawalDetailsService iWithdrawalDetailsService;

    @Autowired
    private BzConfigService bzConfigService;

    @Autowired
    private ITzCollectionInfoService iTzCollectionInfoService;



    @GetMapping("/selectWithdrawalDetails")
    public Result selectWithdrawalDetails(WithdrawalDetails withdrawalDetails, Integer offset){

        try {
            String userId = SecurityUtils.getUser().getUserId();
            withdrawalDetails.setUserId(userId);
            PageInfo<WithdrawalDetails> pageInfo = iWithdrawalDetailsService.selectWithdrawalDetails(withdrawalDetails, offset);
            return Result.success(pageInfo);
        }catch (Exception e){
            return Result.failure(ResultCode.WEB_401,"获取用户信息失败");
        }

    }

    @PostMapping("addWithdrawal")
    public Result addWithdrawal(@RequestBody WithdrawalDetails withdrawalDetails){

        try {

            if(null == withdrawalDetails.getMoney()){
                return Result.failure("请输入提现金额");
            }

            if(null == withdrawalDetails.getCollectionId()){
                return Result.failure("请选择到账渠道");
            }

            BzConfig byType = bzConfigService.getByType(Constant.SWITCH_OPEN);
            if(null != byType){
                if(!byType.getValue().equals("1")){
                    return Result.failure("提现功能暂未开放");
                }
            }

            iWithdrawalDetailsService.addWithdrawalDetails(withdrawalDetails);

            return Result.success();
        }catch (Exception e){
            return Result.failure(e.getMessage());
        }

    }


    @GetMapping("getDetail/{id}")
    public Result getDetail(@PathVariable Long id){
        if(null == id){
            return Result.failure("获取详情失败");
        }
        WithdrawalDetails withdrawalDetails = iWithdrawalDetailsService.getById(id);

        TzCollectionInfo collectionInfo = iTzCollectionInfoService.getById(withdrawalDetails.getCollectionId());
        if(null != collectionInfo){
            String account = collectionInfo.getAccount();
            if(account.length() >= 4){
                String accountr = account.substring(0, 4) + "****" + account.substring(account.length() - 4, account.length());
                collectionInfo.setAccount(accountr);
            }

            String resMobile = collectionInfo.getResMobile();
            String phone = resMobile.substring(0, 3) + "****" + resMobile.substring(resMobile.length() - 4, resMobile.length());
            collectionInfo.setResMobile(phone);

            String idCard = collectionInfo.getIdCard();
            if(idCard.length() >= 4){
                String idCardNo = idCard.substring(0, 4) + "********" + idCard.substring(idCard.length() - 4, idCard.length());
                collectionInfo.setIdCard(idCardNo);
            }
            String fullName = collectionInfo.getFullName();
            if(fullName.length() >=2){
                String fullName2 = fullName.substring(0, 1) + "*" + fullName.substring(fullName.length() - 1, fullName.length());
                collectionInfo.setFullName(fullName2);
            }

        }

        Map<String,Object> result = new HashMap<>();
        result.put("withdrawalDetails",withdrawalDetails);
        result.put("collectionInfo",collectionInfo);
        return Result.success(result);
    }

}
